<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\ProductsAttr;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ProductAttrController extends BannerController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
     $data=   DB::table('products_attr')->orderBy('id','desc')->paginate(10);
     $products=DB::table('products')->select('id','name')->get();
     return $this->success(compact('data','products'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $name=DB::table('products')->where('id',$request->input('products_id'))->select('name')->first();
         $request->merge(['product_name'=>$name->name]);
        if(DB::table('products_attr')->insert($request->all())) return $this->success();
          return $this->apiError();
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $name=DB::table('products')->where('id',$request->input('products_id'))->select('name')->first();
        $request->merge(['product_name'=>$name->name]);
        if(DB::table('products_attr')->where('id',$id)->update($request->all())) return $this->success();
        return $this->apiError();

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        if(DB::table('products_attr')->delete($id)) return $this->success();
        return $this->apiError();
    }
}
